package NiuKe.JianZhiOffer;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * pop():弹出栈顶元素
 * top():获取栈顶元素
 * min():获取栈中最小元素
 *
 * 示例:
 * 输入:    ["PSH-1","PSH2","MIN","TOP","POP","PSH1","TOP","MIN"]
 * 输出:    -1,2,1,-1
 * 解析:
 * "PSH-1"表示将-1压入栈中，栈中元素为-1
 * "PSH2"表示将2压入栈中，栈中元素为2，-1
 * “MIN”表示获取此时栈中最小元素==>返回-1
 * "TOP"表示获取栈顶元素==>返回2
 * "POP"表示弹出栈顶元素，弹出2，栈中元素为-1
 * "PSH-1"表示将1压入栈中，栈中元素为1，-1
 * "TOP"表示获取栈顶元素==>返回1
 * “MIN”表示获取此时栈中最小元素==>返回-1
 *
 * 示例1
 * 输入：
 *  ["PSH-1","PSH2","MIN","TOP","POP","PSH1","TOP","MIN"]
 *
 * 返回值：
 * -1,2,1,-1
 */
public class JZ20 {
    Stack<Integer> stack = new Stack<Integer>();
    public void push(int node) {
        stack.push(node);
    }

    public void pop() {
        stack.pop();
    }

    public int top() {
        int a= stack.pop();
        stack.push(a);
        return a;

    }

    public int min() {

        List<Integer> list = new ArrayList<Integer>();
        int min= top();
        while (!stack.isEmpty()){
            int m = stack.pop();
            min = (min < m)? min:m;
            list.add(m);
        }
        for (int i = list.size()-1;i >= 0; i--){
            stack.push(list.indexOf(i));
        }
        return min;
    }
}
